/**
 * 
 */
package pl.kaczanowscy.tomek.ipdl.strategy;

import pl.kaczanowscy.tomek.ipdl.RoundResult;
import pl.kaczanowscy.tomek.ipdl.Strategy;
import pl.kaczanowscy.tomek.ipdl.enums.Move;

/**
 * GBM - Go By Majority.
 * 
 * @author Tomek Kaczanowski
 * 
 */
public final class GBM implements Strategy {

	private int betrayalCounter = 0;

	/**
	 * GBM plays defection if opponent betrayed more times than cooperated.
	 */
	@Override
	public Move getMove() {
		return (betrayalCounter > 0) ? Move.DEFECTION : Move.COOPERATION;
	}

	/**
	 * GBM checks if opponent betrayed in the last round.
	 * 
	 * @see pl.kaczanowscy.tomek.ipdl.RoundListener#roundEnded(pl.kaczanowscy.tomek.ipdl.RoundResult)
	 */
	@Override
	public void roundEnded(RoundResult result) {
		if (Move.COOPERATION == result.getOpponentMove(this)) {
			betrayalCounter--;
		} else {
			betrayalCounter++;
		}
	}

}
